package com.education.wisdom.controller.app;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.education.common.utils.LogUtil;
import com.education.common.utils.UUIDUtils;
import com.education.wisdom.pojo.Answers;
import com.education.wisdom.pojo.CatalogAppraisal;
import com.education.wisdom.pojo.Paper;
import com.education.wisdom.pojo.Questions;
import com.education.wisdom.service.IAnswersService;
import com.education.wisdom.service.ICatalogAppraisalService;
import com.education.wisdom.service.IPaperService;
import com.education.wisdom.service.IQuestionsService;
import com.education.wisdom.vo.QuestionsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.education.common.core.BaseController.writeJSON;

/**
 * 职业技能鉴定目录体系 信息操作处理
 *
 * @author ruoyi
 * @date 2019-06-19
 */
@Controller
@CrossOrigin
@Slf4j
@ResponseBody
@RequestMapping("/appCatalogAppraisal")
@Api(tags = {"职业技能鉴定板块"})
public class AppCatalogAppraisalController {

    @Autowired
    private ICatalogAppraisalService catalogAppraisalService;

    @Autowired
    private IPaperService paperService;

    @Autowired
    private IQuestionsService questionsService;

    @Autowired
    private IAnswersService answersService;

    /**
     * 职业技能测试目录列表查询
     *
     * @param response
     * @throws IOException
     */
    @GetMapping("/list")
    public void list(HttpServletResponse response) throws IOException {
        log.info(LogUtil.LogInfo("查询职业技能鉴定目录体系", new Date()));
        List<CatalogAppraisal> catalogAppraisals = catalogAppraisalService.selectCatalogAppraisalList(null);
        writeJSON(catalogAppraisals, response, "200", "success");
    }

    /**
     * 查询职业技能鉴定目录下试题
     *
     * @param response
     * @param pageNo
     * @param appraisalCatalogId
     * @throws IOException
     */
    @GetMapping("/queryPaper")
    public void queryPaper(HttpServletResponse response, Integer pageNo, Integer appraisalCatalogId) throws IOException {
        log.info(LogUtil.LogInfo("查询职业技能鉴定目录对应试题", new Date()));
        int pageSize = 10;
        if (appraisalCatalogId != null) {
            PageHelper.startPage(pageNo, pageSize);
            List<Paper> paperList = paperService.selectByAppraisalCatalogId(appraisalCatalogId);
            writeJSON(new PageInfo<>(paperList), response, "200", "success");
        } else {
            PageHelper.startPage(pageNo, pageSize);
            List<Paper> paperList = paperService.selectTitlelist();
            writeJSON(new PageInfo<>(paperList), response, "200", "success");
        }
    }

    @GetMapping("/questionsList")
    public void questionsList(HttpServletResponse response, String paperId) throws Exception {
        log.info(LogUtil.LogInfo("查询试题列表", new Date()));
        Map<String, Object> map = new HashMap<>();
        map.put("paperId", paperId);
        List<QuestionsVo> list = questionsService.selectQuestionsListById(map);
        int count = questionsService.selectCount(map);
        Map<String, Object> map1 = new HashMap<>();
        map1.put("count", count);
        map1.put("list", list);
        writeJSON(map1, response, "200", "成功");
    }

    /**
     * 用户试卷答题
     */
    @PostMapping("/answers")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "djPaperid", value = "试卷编号", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "djUserid", value = "答题者Id", required = true, dataType = "String"),
            @ApiImplicitParam(name = "djQuestionsids", value = "答题ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "djAnswers", value = "答题内容", required = true, dataType = "String"),
            @ApiImplicitParam(name = "djStarttime", value = "开始时间", required = true, dataType = "Date")})
    public void answers(HttpServletResponse response, Integer djPaperid, String djUserid, String djQuestionsids, String djAnswers,
                        Date djStarttime) throws Exception {
        log.info(LogUtil.LogInfo("试卷答题", new Date()));
        BigDecimal b0 = new BigDecimal(0);
        Paper paper = paperService.selectPaperById(djPaperid);
        if (paper != null) {
            String[] questionsids = djQuestionsids.split(",");
            String[] sanswers = djAnswers.split(",");
            if (questionsids.length == sanswers.length) {

                for (int i = 0; i < questionsids.length; i++) {
                    String questionsid = questionsids[i];
                    String sanswer1 = sanswers[i];
                    Questions question = questionsService.selectQuestionsById(questionsid);
                    String stAnswer = question.getStAnswer();
                    BigDecimal stScore = question.getStScore();
                    if (stAnswer.equals(sanswer1)) {//答案正确
                        b0 = b0.add(stScore);
                    }
                }
                Answers answer = new Answers();
                answer.setDjId(UUIDUtils.getByUUId());
                answer.setDjEndtime(new Date());
                answer.setDjStarttime(djStarttime);
                answer.setDjContent(djAnswers);
                answer.setDjPaperid(djPaperid);
                answer.setDjScore(b0);
                answer.setDjUserid(djUserid);
                answersService.insertAnswers(answer);
            }
        }
        writeJSON(b0, response, "200", "成功");
    }

}
